<?php

declare(strict_types=1);

namespace app\common\service\crm;

use app\common\dao\crm\CustomerDao;
use app\common\service\hrm\FilesService;
use app\common\service\sys\DictService;
use app\common\traits\ServiceTrait;
use app\common\dao\crm\RecordDao;

/**
 * 客户跟进记录 服务业务层(Service)
 * @author 吕玉冰
 * @date  2022-07-20
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class RecordService
{
    protected $dao = null;

    public function __construct()
    {
        $this->dao = new RecordDao();
        $this->dictDao = new DictService();
        $this->customerDao = new CustomerDao();
    }

    use ServiceTrait;

    /**
     * lvyubing
     * @param $data
     * @return array
     */
    public function _list_where($data)
    {
        $where = [];
        if (isset($data['relation_id']) && !empty($data['relation_id'])) {
            $where[] = ['relation_id', '=', $data['relation_id']];
        }
        $where[] = ['relation_type', '=', $data['relation_type']];
        $data['where'] = $where;
        return [
            'where' => isset($data['where']) ? $data['where'] : [],
            'order' => isset($data['order']) ? $data['order'] : [],
            'field' => isset($data['field']) ? $data['field'] : ['*'],
        ];
    }


    /**
     * 格式化数据
     */
    public function _list_format($data)
    {
        //获取字典数据
        $where = [];
        $where[] = ["pid", "=", "122"];
        $list = $this->dictDao->getListAll($where);
        $follow_list = [];
        foreach ($list as $key => $value) {
            $follow_list[$value['value']] = $value['name'];
        }

        $where = [];
        $where[] = ["pid", "=", "126"];
        $list = $this->dictDao->getListAll($where);
        $followStatus = [];
        foreach ($list as $key => $value) {
            $followStatus[$value['value']] = $value['name'];
        }

        foreach ($data as $key => $value) {
            $data[$key]['follow_time'] = date("Y-m-d H:i:s", $value['follow_time']);
            $data[$key]['follow_type_name'] = $follow_list[$value['follow_type']];
            $data[$key]['follow_name'] = $followStatus[$value['follow']];
            if (isset($value['files_id'])) {
                $data[$key]['files_id'] = _img($value['files_id']);
            }
        }
        return $data;
    }


    /**
     * 保存表单数据
     * @param $data
     * @return array
     */
    public function saveForm($data)
    {
        if (empty($data)) {
            return _err(1002);
        }
        $data['follow_time'] = time();
        if (isset($data['id']) && !empty($data['id'])) {
            $result = $this->dao->update($data['id'], $data);
            if (isset($data['files_id'])) {
                (new FilesService())->add([
                    'table_name' => 'crm_record',
                    'table_id' => $data['id'],
                    'files_id' => $data['files_id']
                ]);
            }
            switch ($data['relation_type']) {
                case 1:
                    $this->customerDao->update($data['relation_id'], ['follow' => $data['follow']]);
                    break;
            }
            if ($result) {
                return _ok(1021);
            }
        } else {
            $result = $this->dao->add($data);
            if ($result) {
                $return = _ok(1023);
                if (!empty($result->id)) {
                    if (isset($data['files_id'])) {
                        (new FilesService())->add([
                            'table_name' => 'crm_record',
                            'table_id' => $result->id,
                            'files_id' => $data['files_id']
                        ]);
                    }
                    $return['data'] = $result->id;
                    switch ($data['relation_type']) {
                        case 1:
                            $this->customerDao->update($data['relation_id'], ['follow' => $data['follow']]);
                            break;
                    }

                }
                return $return;
            }
        }
        return _err(1005);
    }
}
    